<?php
require_once 'MyPdoCounty.class.php';
class TableShopFreeOrder extends MyPdoCounty{
	private $tableName = 'shop_free_order';

	//查询指定砍价商品的非失效订单总数
	function _getFreeGoodsOrderCount($free_id){
		$sql = "select count(id) as count from {$this->tableName} where free_id = ? and stat in(1,2)";
		return $this->dql($sql,array($free_id),1);
	}


	/*
		### 获取指定商户的砍价订单
		$store_id 	商户id
		$selType	订单状态
		$freeGoodsId 如果有值，则查询价商品id关联的订单

	*/
	function _getFreeOrderListForStore($store_id,$selType,$search,$freeGoodsId){
		if($store_id){
			if($search){
				$sql = "select o.id,o.order_numb,o.ctime,o.stat,g.title,g.price,g.banner from (shop_free_order as o join shop_free as f on o.free_id = f.id) join shop_goods as g on f.goods_id = g.id where o.store_id = ? and o.stat = ? and o.order_numb like ?";
				$arr = array($store_id,$selType,"%{$search}%");
			}else{
				$sql = "select o.id,o.order_numb,o.ctime,o.stat,g.title,g.price,g.banner from (shop_free_order as o join shop_free as f on o.free_id = f.id) join shop_goods as g on f.goods_id = g.id where o.store_id = ? and o.stat = ?";
				$arr = array($store_id,$selType);
			}
			if($freeGoodsId){
				$where = ' and o.free_id = ?';
				$sql .= $where;
				array_push($arr,$freeGoodsId);
			}
			return $this->dql($sql,$arr);
		}else{
			if($search){
				$sql = "select o.id,o.order_numb,o.ctime,o.stat,g.title,g.price,g.banner from (shop_free_order as o join shop_free as f on o.free_id = f.id) join shop_goods as g on f.goods_id = g.id where o.stat = ? and o.order_numb like ?";
				$arr = array($selType,"%{$search}%");
			}else{
				$sql = "select o.id,o.order_numb,o.ctime,o.stat,g.title,g.price,g.banner from (shop_free_order as o join shop_free as f on o.free_id = f.id) join shop_goods as g on f.goods_id = g.id where o.stat = ?";
				$arr = array($selType);
			}
			if($freeGoodsId){
				$where = ' and o.free_id = ?';
				$sql .= $where;
				array_push($arr,$freeGoodsId);
			}
			return $this->dql($sql,$arr);
		}
	}





	/*
		### 创建砍价订单
			段位 份数基价   份数    尾刀份数
			1000					1000
			320
			160  6.5		124-246 80
			80   5.5毛一份	74-160	54
			40   4.5毛一份	46-88	30
			20   3.5毛一份	31-57	20
			10   2.5毛一份	24-40	18	
			5    1.5毛一份	20-33	15
			2    1毛一份	10-20 	6



			1：判断总价所处段位并计算总份  总价/基价 = 总刀数 
			2：计算尾刀份数   总份数的75%
			3: 计算去掉尾刀的余额：总价 - 尾刀数 * 0.01 = 去尾余额
			4：计算余刀数 总刀数 - 尾刀数 = 余刀数
			5：计算随机额 遍历余刀次数 每次获取随机额为余刀额



	*/
	function _createFreeOrder($free_id,$price,$store_id){
		$order_numb = date('YmdHis').$store_id.$free_id.rand(111,9999);	//生成订单号
		$name = 'name_'.rand(11111,99999);	//名字
		$header = '/base.jpg';	//头像
		$openid = 'openid_'.time();	//后期需要传入

		//分割砍价
		//$price = 33.33;
		if($price <= 2){
			$priceBase = 0.1;	//每份基数
			$microCount = 6;		//尾刀数
		}else if($price <= 5){
			$priceBase = 0.15;
			$microCount = 15;
		}else if($price <= 10){
			$priceBase = 0.25;
			$microCount = 18;
		}else if($price <= 20){
			$priceBase = 0.35;
			$microCount = 20;
		}else if($price <= 40){
			$priceBase = 0.45;
			$microCount = 30;
		}else if($price <= 80){
			$priceBase = 0.55;
			$microCount = 54;
		}else{
			$priceBase = 0.65;
			$microCount = 6;
		}

		$count = ceil($price / $priceBase);	//总刀数
		$userFreePrice = round($price * (rand(2,4)/10));	//发起用户的砍价
		$ye = $price - $microCount * 0.01 - $userFreePrice;	//去尾余额
		$yei1 = ceil($ye/4);	//大刀金额标记，当余额小于该值时，停止大刀的计算
		$ydCount = $count - $microCount;			//余刀



		$pList = array();		//砍价的每份价格列表
		for($i=0;$i<$ydCount-1;$i++){		//这里减一是留给发起订单的用户自己砍的一刀
			if($i < $ydCount-2){ //非最后一刀
				if($ye > $yei1){	//计算大刀
					$tmp = round($ye * 0.2,2);
				}else{	//计算平刀
					//echo $ye.'----'.($ydCount - $i).'<br>';
					//$tmp = round($ye/($ydCount - $i));
					$t1 = $ye/$ydCount;//平均数
					$t2 = $t1 * (rand(1,30)/10);
					$t3 = $t2>0.05?$t2:0.05;
					$tmp = round($t3,2);
					//echo $t1.'==='.$t2.'===='.$tmp;exit;
				}
			}else{
				$tmp = round($ye,2);	//最后一刀
			}
			$pList[] = $tmp;
			$ye -= $tmp;
		}

		shuffle($pList);
		//填充最小值
		for($i=0;$i<$microCount;$i++){
			array_unshift($pList,0.01);
		}
		// $t1 = $userFreePrice;
		// foreach($pList as $v){
		// 	$t1 += $v;
		// }
		// echo $t1.'<<'.max($pList).'>>>'.min($pList);
		// dump($pList,1);


		$sql = "insert ignore into {$this->tableName}(order_numb,openid,name,header,free_id,store_id,price_list,ctime,stat) values(?,?,?,?,?,?,?,?,2)";

		$arr = array(
			$order_numb,
			$openid,
			$name,
			$header,
			$free_id,
			$store_id,
			json_encode($pList),
			time()
		);
		$order_id = $this->dml($sql,$arr);
		if($order_id){	//订单创建成功后，将创建者的砍价记录的帮砍表中
			$tableShopFreeOrderUser = new TableShopFreeOrderUser();
			$res = $tableShopFreeOrderUser->_FreeOrderUserAdd($order_id,$openid,$name,$header,$userFreePrice);
			if($res){
				return 'ok';
			}else{	//帮砍用户添加失败，删除订单
				$sql2 = "delete from {$this->tableName} where id = ?";
				$this->dml($sql2,array($order_id));
				return 'err3';	//记录帮砍信息失败
			}
		}else{
			return 'err2';	//创建订单失败

		}
	}
}
